package com.eascs.admin.sys.permission.controller;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.eascs.admin.sys.permission.form.OrgUserRoleForm;
import com.eascs.admin.sys.permission.form.SysRoleForm;
import com.eascs.common.util.StringUtil;
import com.eascs.site.controller.BaseRestController;
import com.eascs.site.view.BaseRestResult;
import com.eascs.web.sso.User;
import com.eascs.web.sso.UserHolder;
import com.eascs.workflow.common.entity.sys.ActOrgEntity;
import com.eascs.workflow.common.entity.sys.SysRole;
import com.eascs.workflow.common.entity.sys.SysRoleResources;
import com.eascs.workflow.common.service.sys.SysOrgService;
import com.eascs.workflow.common.service.sys.SysPermissionService;
import com.eascs.workflow.common.vo.ModelResult;

@RestController
@RequestMapping("/act/role/rest")
public class RoleRestController extends BaseRestController{
	@Reference
	private SysPermissionService sysPermissionService;
	@Reference
	private SysOrgService sysOrgService;
	
	
	@RequestMapping(value = "/queryRole", method = RequestMethod.GET)
	public BaseRestResult<SysRole> queryRole(SysRoleForm form) {
		SysRole sysRole = SysRoleConvert(form);
		ModelResult<SysRole> modelResult=sysPermissionService.selectRolesByRole(sysRole);
		SysRole result=modelResult.getModel();
		BaseRestResult<SysRole> baseRestResult=BaseRestResult.buildRestResult(result);
		return baseRestResult;
	}
	
	@RequestMapping(value = "/queryUserOrgList", method = RequestMethod.GET)
	public BaseRestResult<List<ActOrgEntity>> queryOrgUser(String userNo){
		ModelResult<ActOrgEntity> modelResult = sysOrgService.getActOrgByUserNo(userNo);
		ActOrgEntity actOrg = modelResult.getModel();
		if(actOrg == null){
			return null;
		}
		List<ActOrgEntity> list = sysOrgService.getAllOrgParentList(actOrg.getOrgId()).getModel();
		BaseRestResult<List<ActOrgEntity>> baseRestResult = BaseRestResult.buildRestResult(list);
		return baseRestResult;
	}
	
	@RequestMapping(value = "/updateOrSave", method = RequestMethod.POST)
	public BaseRestResult<String> updateOrSave(SysRoleForm form,String roleResJson) {
		try {
			List<SysRoleResources> roleResList=JSON.parseArray(roleResJson, SysRoleResources.class);
			SysRole sysRole = SysRoleConvert(form);
			roleResListConvert(roleResList);
			if(StringUtils.isNoneBlank(sysRole.getId()) && !sysRole.getId().equals("null")){
				sysPermissionService.updateRole(sysRole);
			}else {
				sysRole = sysPermissionService.insertRoleReturnRole(sysRole).getModel();
				for (SysRoleResources item : roleResList) {
					item.setRoleId(sysRole.getId());
				}
			}
			if(roleResList.size() > 0){
				sysPermissionService.insertRoleResBatch(roleResList).getModel();
			}
		} catch (Exception e) {
			return BaseRestResult.warn("保存失败!");
		}
		return BaseRestResult.warn("保存成功!").andRefresh();
	}
	
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public BaseRestResult<String> update(SysRoleForm form) {
		SysRole sysRole = SysRoleConvert(form);
		ModelResult<Integer> modelResult=sysPermissionService.updateRole(sysRole);
		Integer result=modelResult.getModel();
		if(result > 0){
			return BaseRestResult.warn("修改成功!").andRefresh();
		}else{
			return BaseRestResult.warn("修改失败!");
		}
	}
	
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public BaseRestResult<String> save(SysRoleForm form) {
		SysRole sysRole = SysRoleConvert(form);
		ModelResult<Integer> modelResult=sysPermissionService.insertRole(sysRole);
		Integer result=modelResult.getModel();
		if(result > 0){
			return BaseRestResult.warn("添加成功!").andRefresh();
		}else{
			return BaseRestResult.warn("新增失败!");
		}
	}
	
	@RequestMapping(value = "/saveOrgUserRoleByempno", method = RequestMethod.POST)
	public BaseRestResult<String> saveOrgUserRoleByempno(OrgUserRoleForm form) {
		String orgId = form.getOrgId();
		String userNo = form.getUserNo();
		String roleId = form.getRoleId();
		if(StringUtil.hasText(orgId)){
			ActOrgEntity orgEntity = sysOrgService.getActOrgByUserNo(userNo).getModel();
			if(null == orgEntity){
				return BaseRestResult.warn("新增失败，未配置员工所属组织，请联系管理员!");
			}
		}
		User user = UserHolder.getUser();
		Integer result=sysPermissionService.insertOrgUserRoleByempno(user ,userNo, roleId, orgId);
		if(result > 0){
			return BaseRestResult.warn("添加成功!").andRefresh();
		}if(result == -1){
			return BaseRestResult.warn("新增失败，未配置员工所属组织，请联系管理员!");
		}if(result == -2){
			return BaseRestResult.warn("新增失败，不能重复添加!");
		}else{
			return BaseRestResult.warn("新增失败!");
		}
	}
	
	@RequestMapping("/delete-{id}")
    public BaseRestResult<String> delete(Model model, @PathVariable String id) {
		SysRole sysRole=new SysRole();
		sysRole.setId(id);
		ModelResult<Integer> modelResult=sysPermissionService.deleteRole(sysRole);
		Integer result=modelResult.getModel();
		if(result > 0){
			return BaseRestResult.warn("删除成功!").andRefresh();
		}else{
			return BaseRestResult.warn("删除失败!");
		}
	}
	
	@RequestMapping("/userOrgRole/delete-{id}")
    public BaseRestResult<String> deleteUserOrgRole(Model model, @PathVariable String id) {
		ModelResult<Integer> modelResult=sysPermissionService.delUserRole(id);
		Integer result=modelResult.getModel();
		if(result > 0){
			return BaseRestResult.warn("删除成功!").andRefresh();
		}else{
			return BaseRestResult.warn("删除失败!");
		}
	}
	
	private SysRole SysRoleConvert(SysRoleForm roleform){
		User user=UserHolder.getUser();
		SysRole role=new SysRole();
		role.setId(roleform.getId());
		role.setRoleName(roleform.getRoleName());
		role.setRoleCode(roleform.getRoleCode());
		role.setDescription(roleform.getDescription());
		role.setRoleLevel(roleform.getRoleLevel());
		role.setUpdateTime(new Date());
		role.setCreateTime(new Date());
		role.setUpdator(user.getAccount());
		role.setCreator(user.getAccount());
		role.setRoleCategory(roleform.getRoleCategory());
		return role;
	}
	
	private void roleResListConvert(List<SysRoleResources> roleResList){
		User user=UserHolder.getUser();
		for (SysRoleResources sysRoleResources : roleResList) {
			sysRoleResources.setUpdateTime(new Date());
			sysRoleResources.setCreateTime(new Date());
			sysRoleResources.setUpdator(user.getAccount());
			sysRoleResources.setCreator(user.getAccount());
		}
		
	}
	
}
